package yxy.game.pm2.command.bandit;

import org.joda.time.DateTime;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.crusadedateduties;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_冲榜;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BanditMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.bandit.Bandit;
import yxy.game.pm2.module.bandit.BanditHandle;
import yxy.game.pm2.module.bandit.BanditModule;

import java.util.ArrayList;

/** 剿匪 剿匪奖励领取 */
@MessageType(proto = Command.BanditBonusReceive, classtype = BanditMessage.BanditBonusReceiveRequest.class)
public class CMD_BanditBonusReceive extends Command<BanditMessage.BanditBonusReceiveRequest> {

	@Override
	public void execute(MessageSource messageSource, BanditMessage.BanditBonusReceiveRequest request) {
		String playerCode = messageSource.getTag();
		int taskId = request.getId();

		BanditMessage.BanditBonusReceiveResponse.Builder response = BanditMessage.BanditBonusReceiveResponse.newBuilder();

		BanditModule banditModule = BanditModule.get(playerCode);
		try {
			if (banditModule.tryLock()) {
				if (taskId == -1) {
					// 全部领取
					ArrayList<Bandit> canTakeList = new ArrayList<>();
					DateTime now = DateTime.now();
					for (Bandit bandit : banditModule.getTaskList()) {
						if (bandit.getEndTime() != null && bandit.getEndTime().isBefore(now)) {
							canTakeList.add(bandit);
						}
					}
					if (canTakeList.size() <= 0) {
						send(messageSource, response, Result.BanditTaskNoGet);// 没有任务
						return;
					}
					ArrayList<Currency> rewards = new ArrayList<>();
					for (Bandit bandit : canTakeList) {
						crusadedateduties data = DataCenter.getData(bandit.getDataId(), crusadedateduties.class);
						Currency reward = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
						rewards.add(reward);

						bandit.setDataId(0);
						bandit.setEndTime(null);
						bandit.setSendingHeroCode(null);
					}
					banditModule.set();

					rewards = new ArrayList<>(Currency.merge(rewards));
					new CurrencyHandle().addCurrency(playerCode, CCReason.剿匪, rewards);
					response.addAllRewards(new CurrencyHandle().packItems(rewards));

					//todo 每次循环的前半小时内不开启榜
					ActiveModule_冲榜 am = ActiveModule_冲榜.get(playerCode, false);
					DateTime begin = am.getRunend().minusDays(6).withHourOfDay(0).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
					if (!DateTime.now().isBefore(begin)) {
						RankRushHandle.addRankRushData(playerCode, canTakeList.size(), RankRushEnum.冲榜_剿匪次数.getType());
					}
				} else {
					Bandit bandit = banditModule.task(taskId);
					if (bandit == null || bandit.getEndTime() == null) {
						send(messageSource, response, Result.BanditTaskCantReceive);// 剿匪任务领取不了
						return;
					}
					if (bandit.getEndTime().isAfter(DateTime.now())) {
						send(messageSource, response, Result.BanditTaskCantReceive);// 剿匪任务领取不了
						return;
					}
					crusadedateduties data = DataCenter.getData(bandit.getDataId(), crusadedateduties.class);
					Currency reward = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
					new CurrencyHandle().addCurrency(playerCode, CCReason.剿匪, reward);

					bandit.setDataId(0);
					bandit.setEndTime(null);
					bandit.setSendingHeroCode(null);
					banditModule.set();

					response.addRewards(new CurrencyHandle().packItem(reward));

					//todo 每次循环的前半小时内不开启榜
					ActiveModule_冲榜 am = ActiveModule_冲榜.get(playerCode, false);
					DateTime begin = am.getRunend().minusDays(6).withHourOfDay(0).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
					if (!DateTime.now().isBefore(begin)) {
						RankRushHandle.addRankRushData(playerCode, 1, RankRushEnum.冲榜_剿匪次数.getType());
					}
				}

				send(messageSource, response, Result.Success);

				new BanditHandle().updateBanditTip(playerCode);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			banditModule.unlock();
		}
	}

	private void send(MessageSource messageSource, BanditMessage.BanditBonusReceiveResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}